package com.core.family.mybatisplus;

/**
 * @program: YJ-Admin
 * @description:
 * @author: hl
 * @create: 2024-05-30 16:06
 **/

import com.github.yulichang.base.MPJBaseMapper;
import com.github.yulichang.base.MPJBaseService;
import org.springframework.beans.factory.annotation.Autowired;

public class MapperAndServiceUtil {

    public static <T> MPJBaseService getService(final Class<T> t) {
        String beanName = getBeanName(t) + "ServiceImpl";
        if (!SpringUtil.containsBean(beanName)) {
            MapperAndServiceConfig config = SpringUtil.getBean(MapperAndServiceConfig.class);
            config.setup();
        }
        return SpringUtil.getBean(beanName);
    }

    public static <T> MPJBaseMapper getMapper(final Class<T> t) {
        String beanName = getBeanName(t) + "Mapper";
        if (!SpringUtil.containsBean(beanName)) {
            MapperAndServiceConfig config = SpringUtil.getBean(MapperAndServiceConfig.class);
            config.setup();
        }
        return SpringUtil.getBean(beanName);
    }

    private static String getBeanName(final Class t) {
        return t.getSimpleName().substring(0, 1).toLowerCase() + t.getSimpleName().substring(1);
    }
}
